package com.itheima.admin.controller.v1;

import com.itheima.admin.mapper.AdminPermissionMapper;
import com.itheima.model.admin.pojos.AdminPermission;
import com.itheima.model.common.dtos.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 权限管理控制器
 * 
 * @author itheima
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/permission")
@Api(tags = "权限管理")
public class PermissionController {

    @Autowired
    private AdminPermissionMapper permissionMapper;

    @GetMapping("/list")
    @ApiOperation("查询所有权限列表")
    public ResponseResult listAllPermissions() {
        List<AdminPermission> permissions = permissionMapper.selectList(null);
        return ResponseResult.okResult(permissions);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID查询权限详情")
    public ResponseResult getPermissionById(@PathVariable Integer id) {
        AdminPermission permission = permissionMapper.selectById(id);
        return ResponseResult.okResult(permission);
    }

    @PostMapping
    @ApiOperation("创建权限")
    public ResponseResult createPermission(@RequestBody AdminPermission permission) {
        permissionMapper.insert(permission);
        return ResponseResult.okResult(permission);
    }

    @PutMapping("/{id}")
    @ApiOperation("更新权限")
    public ResponseResult updatePermission(@PathVariable Integer id, @RequestBody AdminPermission permission) {
        permission.setId(id);
        permissionMapper.updateById(permission);
        return ResponseResult.okResult(permission);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除权限")
    public ResponseResult deletePermission(@PathVariable Integer id) {
        permissionMapper.deleteById(id);
        return ResponseResult.okResult("删除成功");
    }
}

